package net.mingsoft.basic.strategy;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.models.auth.In;
import net.mingsoft.basic.biz.IManagerBiz;
import net.mingsoft.basic.biz.IRoleBiz;
import net.mingsoft.basic.constant.e.SessionConstEnum;
import net.mingsoft.basic.entity.ManagerEntity;
import net.mingsoft.basic.entity.ManagerSessionEntity;
import net.mingsoft.basic.entity.RoleEntity;
import net.mingsoft.basic.exception.BusinessException;
import net.mingsoft.basic.util.BasicUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 管理员登录列表
 *
 * @author Administrator
 * @version 创建日期：2020/11/18 18:12<br/>
 * 历史修订：<br/>
 */
public class ManagerLoginStrategy implements ILoginStrategy {


    @Autowired
    private IManagerBiz managerBiz;

    @Autowired
    private IRoleBiz roleBiz;

    @Override
    public Boolean login(ManagerEntity manager) {
        boolean rememberMe = BasicUtil.getBoolean("rememberMe");
        if (manager == null || StringUtils.isEmpty(manager.getManagerName()) || StringUtils.isEmpty(manager.getManagerPassword())) {
            return false;
        }
        // 根据账号获取当前管理员信息
        ManagerEntity newManager = new ManagerEntity();
        newManager.setManagerName(manager.getManagerName());
        ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager);
        if (_manager == null) {
            // 系统不存在此用户
            throw new BusinessException("账号不存在");
        } else {
            // 判断当前用户输入的密码是否正确
            if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) {

                //站群超级管理员
                if (_manager.getManagerAdmin()==null || !_manager.getManagerAdmin().equalsIgnoreCase("superadmin")) {
                    //检查是否是属于当前站点的管理员
                    RoleEntity role = new RoleEntity();
                    role.setId(String.valueOf(_manager.getRoleId()));
                    List<Map> _roles = (List<Map>)roleBiz.excuteSql("select app_id from role where id="+_manager.getRoleId());
                    if (_roles == null || BasicUtil.getApp().getAppId() != Integer.parseInt(_roles.get(0).get("app_id").toString())) {
                        throw new BusinessException("账号登陆失败");
                    }
                }

                //创建管理员session对象
                ManagerSessionEntity managerSession = new ManagerSessionEntity();
                // 压入管理员seesion
                BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession);
                BeanUtils.copyProperties(_manager, managerSession);
                Subject subject = SecurityUtils.getSubject();
                UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword());
                upt.setRememberMe(rememberMe);
                subject.login(upt);
                return true;
            } else {
                // 密码错误
                throw new BusinessException("账号密码错误");
            }
        }
    }
}
